Page({
  data: {
    messages: [
    ],
    unread_group_message_count: 0,
    remote_src: getApp().globalData.remote_src,
    userInfo: getApp().globalData.userInfo,
    // 添加三种类型消息的未读数量
    unreadActivityCount: 0,
    unreadInteractionCount: 0,
    unreadSystemCount: 0
  },
  // 跳转到互动提示页面
  goToInteractionNotify() {
    wx.navigateTo({
      url: '/pages/message/interactnotify/interactnotify'
    });
  },
// ... existing code ...
   // 跳转到活动通知页面
   goToActivityNotify() {
    wx.navigateTo({
      url: '/pages/message/activitynotify/activitynotify'
    });
  },
  onShow() {
    // 每次页面显示时获取最新群聊列表
    this.getConversationList();
    this.getUnreadMessageCount();
  },
  // 获取未读消息数量
  getUnreadMessageCount() {
    const websiteUrl = getApp().globalData.onlineService + "/api/message/message_unread_count";
    wx.request({
      url: websiteUrl,
      method: 'GET',
      header: {
          'Authorization': `Bearer ${this.data.userInfo.token}`
      },
      success: (res) => {
        if (res.data.code === 200) {
          this.setData({
            unreadActivityCount: res.data.data[1] || 0,  // 活动消息
            unreadInteractionCount: res.data.data[2] || 0,  // 互动消息
            unreadSystemCount: res.data.data[3] || 0  // 系统消息
          });
        }
      },
      fail: (err) => {
        console.log('获取未读消息数量失败', err);
        console.error('获取未读消息数量失败', err);
      }
    });
  },
   // 获取所有会话列表
  getConversationList() {
    if (!wx.$TUIKit) {
      console.error('TIM SDK 未初始化');
      return;
    }

    wx.$TUIKit.getConversationList().then(res => {
      console.log('获取会话列表成功', res.data.conversationList);
      // 过滤只保留群组会话
      const groupConversations = res.data.conversationList.filter(conversation => 
        conversation.type === 'GROUP'
      );
      const conversationList =groupConversations.map(conversation => ({
        conversationID: conversation.conversationID,
        type: conversation.type, // 会话类型，C2C 表示单聊，GROUP 表示群聊
        userProfile: conversation.userProfile, // 用户资料，单聊时有效
        groupProfile: conversation.groupProfile, // 群资料，群聊时有效
        lastMessage: conversation.lastMessage, // 最后一条消息
        people: conversation.groupProfile.memberCount, // 群成员数
        unread: conversation.unreadCount, // 未读消息数
        title: conversation.type === 'GROUP' ? 
              conversation.groupProfile.name : 
              conversation.userProfile.nick || conversation.userProfile.userID
      }));

      this.setData({
        messages: conversationList,
        unread_group_message_count: conversationList.reduce((total, group) => total + group.unread, 0)
      });
    }).catch(err => {
      console.error('获取会话列表失败', err);
      wx.showToast({
        title: '获取会话列表失败',
        icon: 'none'
      });
    });
  },
  getGroupList() {
    // 确保 TIM SDK 已经准备就绪
    if (!wx.$TUIKit) {
      console.error('TIM SDK 未初始化');
      return;
    }

    wx.$TUIKit.getGroupList().then(res => {
      console.log('获取群组列表成功', res.data.groupList);
      const groupList = res.data.groupList.map(group => ({
        group_id: group.groupID,
        title: group.name,
        people: group.memberCount,
        unread: group.unreadCount || 0
      }));
      this.data.unread_group_message_count = groupList.reduce((total, group) => total + group.unread, 0);
      
      this.setData({
        messages: groupList
      });
    }).catch(err => {
      console.error('获取群组列表失败', err);
      wx.showToast({
        title: '获取群聊失败',
        icon: 'none'
      });
    });
  },
  enterChatRoom(e) {
    const groupId = e.currentTarget.dataset.groupId;
    console.log(`/package_chat/pages/chat/chat?${groupId}`);
    wx.navigateTo({
      url: `/package_chat/pages/chat/chat?conversationID=${groupId}`
    });
  }
})